from interface.member import MemberInterface
from utils.get_keyword import GetKeyword
from utils.request_method import SendMethod
from utils.mysql_tools import DBtools


class ShopProcess:


    def __init__(self,headers):
        self.url='http://47.108.206.100:8085'
        self.headers = headers # 获取登录的用户令牌


    # 登录
    def user_login(self, body):
        method = 'post'
        url = self.url + '/sso/login'
        return SendMethod.send_method(method, url, data=body)



    #添加商品到购物车
    def add_cart(self,params):
        method='post'
        url = self.url + '/cart/add'
        return SendMethod.send_method(method,url,json=params,headers=self.headers)

    def select_cart(self):
        sql='select * from oms_cart_item where member_id=2283 '
        res=DBtools.select_one(sql)
        print(res)

    #添加收货地址
    def add_address(self,params):
        method ='post'
        url=self.url + '/member/address/add'
        return SendMethod.send_method(method,url,json=params,headers=self.headers)

    #获取收货地址详情
    def get_address_info(self):
        method='get'
        url=self.url+'/member/address/3962'
        return SendMethod.send_method(method,url,headers=self.headers)


    #获取购物车列表
    def get_cart_list(self):
        method='get'
        url=self.url + '/cart/list'
        return SendMethod.send_method(method,url,headers=self.headers)

    #确认订单
    def comfirm_order(self,body):
        method='post'
        url=self.url +'/order/generateConfirmOrder'
        playload = [body]
        return SendMethod.send_method(method,url,json=playload,headers=self.headers)

    # 根据购物车生成订单
    def generate_order(self,card_id):
        method='post'
        url=self.url + '/order/generateOrder'
        params={
                "cartIds": [
                    card_id
                ],
                "memberReceiveAddressId": 3026,
                "payType": 0
        }
        return SendMethod.send_method(method,url,json=params,headers=self.headers)

    #支付成功的回调
    def success_pay(self,params):
        method='post'
        url= self.url +'/order/paySuccess'
        return SendMethod.send_method(method,url,data=params,headers=self.headers)





















if __name__ == '__main__':

    test_data={
        "createDate":"2022-10-18T07:46:09.749Z",
        "deleteStatus": 0,
        "id": 0,
        "memberId": 23,
        "memberNickname": "string",
        "modifyDate": "2022-10-18T07:46:09.749Z",
        "price": 2699,
        "productAttr": '[{"key":"颜色","value":"蓝色"},{"key":"容量","value":"32G"}]',
        "productBrand": "酷派",
        "productCategoryId": 19,
        "productId": 27,
        "productName": "酷派1",
        "productPic": "http://www.abc.com",
        "productSkuCode": '201808270027003',
        "productSkuId": 100,
        "productSn": "7437788",
        "productSubTitle": "AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待手机 双卡双待",
        "quantity":1
    }
    test_data1={
        "city": "武汉",
        "defaultStatus": 0,
        "detailAddress": "江夏区",
        "id": 27,
        "memberId": 2293,
        "name": "string",
        "phoneNumber": "13277799901",
        "postCode": "string",
        "province": "string",
        "region": "string"
    }
    login_data={
        'username':'黑麦面包',
        'password':'123456'

    }
    headers = MemberInterface().get_token_head(login_data)
    sp=ShopProcess(headers)

    # print(sp.add_cart(test_data))#添加购物车
    # sp.select_cart()
    # print(sp.add_address(test_data1))#添加收货地址

    # print(sp.get_address_info())
    # print(sp.get_cart_list())

    # print(sp.comfirm_order(4069))#确认订单
    print(sp.generate_order(4069))  # 生成订单
    playload={
        'orderId':5046,
        'payType':0
    }
    # print(sp.success_pay(playload))
